
const $api = require('./utils/request.js');

var baseURL = "https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp";
var apiUrl = "http://172.16.30.88:8181";
const accountInfo = wx.getAccountInfoSync();
const env = accountInfo.miniProgram.envVersion
if (env == 'release') {
  apiUrl = "https://eicrosoft.com:8181" // 正式版
} else if (env == 'trial') {
  apiUrl = "https://eicrosoft.com:8181"; // 体验版
} else {
  apiUrl = "http://172.16.30.88:8181"; // 开发版
}



var apiUrMock = {
  demo: {
    hospital_list: baseURL + "/hospital"
  },
  catetory: {
    list: baseURL + "/catetory/list",
    save: baseURL + "/catetory/save"
  }
}
class StorageKeys {
  static USERINFO = 'userinfo-' + wx.getAccountInfoSync().miniProgram.envVersion
  static TOKEN = 'token-' + wx.getAccountInfoSync().miniProgram.envVersion
  static CATEGORY_LIST = 'category-list'
  static LOCATION_LIST = 'location-list'
}

const uploadFile = (tempFilePath) => {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      header: {
        memberId: wx.getStorageSync(StorageKeys.TOKEN) // 从缓存读取 token
      },
      url: apiUrl + '/upload/uploadFile',
      filePath: tempFilePath,
      name: 'file',
      formData: {}, // 可附加其他表单数据
      success: (res) => {
        try {
          const fileData = JSON.parse(res.data);
          if (fileData.code === 1) {
            resolve(fileData.data); // 返回图片 URL
          } else {
            reject(new Error(fileData.message || '上传失败'));
          }
        } catch (error) {
          reject(new Error('解析响应数据失败'));
        }
      },
      fail: (err) => {
        reject(new Error('上传请求失败'));
      }
    });
  });
};




const getCategoryList = function () {
  var obj = {
    memberId__eq: wx.getStorageSync(StorageKeys.TOKEN)
  };
  $api.post(apiUrl + '/category/list', obj)
    .then(res => {
      //请求成功  
      wx.setStorageSync(StorageKeys.CATEGORY_LIST, res);
    })
    .catch(err => {
      //请求失败
      console.info(err)
      wx.showModal({
        title: '提示',
        content: JSON.stringify(err),//'这是一个模态弹窗',
        showCancel: false,
      })
    })
}

const getLocationList = function () {
  var obj = {
    memberId__eq: wx.getStorageSync(StorageKeys.TOKEN)
  };
  $api.post(apiUrl + '/location/list', obj)
    .then(res => {
      //请求成功  
      wx.setStorageSync(StorageKeys.LOCATION_LIST, res);
    })
    .catch(err => {
      //请求失败
      console.info(err)
      wx.showModal({
        title: '出错',
        content: JSON.stringify(err),//'这是一个模态弹窗',
        showCancel: false,
      })
    })
}

const developing = function () {
  wx.showModal({
    title: '提示',
    content: '开发中...',//'这是一个模态弹窗',
    showCancel: false,
  })
}

export {
  apiUrl,
  apiUrMock,
  StorageKeys,
  getCategoryList,
  getLocationList,
  developing,
  uploadFile 
}  
